﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CsharpCodeGenerator
{
    public class DalGenerator
    {
        #region SelectAllRows
        public static string SelectAllRows(string MethodName, string TableName, bool Static)
        {
            string strStatic = (Static ? "static " : "");
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format(CsharpHelper.DalClass, TableName, strStatic));
            

            string SpName = TableName + "SelectAllRows";

            sb.AppendLine(string.Format(CsharpHelper.DalSelectAll, strStatic, TableName + MethodName, SpName));

            return sb.ToString();
        }
        #endregion

        #region SelectRow
        public static string SelectRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            StringBuilder Code = new StringBuilder();
            string strStatic = (Static ? "static " : "");
            string spName = TableName + MethodName;

            string Params = string.Empty;
            string newParams = string.Empty;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (int.Parse(item.IsIdentity) != 0 || int.Parse(item.IsIndex) != 0)
                    {
                        Params += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        newParams += item.ColumnName + ", ";
                    }
                }
            }
            Params = (Params.Length > 0 ? Params = Params.Remove(Params.Length - 2, 2) : "");//Params.Remove(Params.Length - 1, 1);
            newParams = newParams.Length > 1 ? newParams.Remove(newParams.Length - 2, 2) : "";
            Code.AppendLine(string.Format(CsharpHelper.DalSelectRow, strStatic, spName, Params, newParams));

            return Code.ToString();
        }
        #endregion

        #region SelectRowByForeignKey
        public static string SelectRowByForeignKey(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            StringBuilder Code = new StringBuilder();
            string strStatic = (Static ? "static " : "");
            string spName = TableName + MethodName;

            foreach (var item in Columns)
            {
                
                if (item.TableName == TableName)
                {
                    if (item.IsForeignKey != null && int.Parse(item.IsForeignKey) != 0)
                    {
                        string Params = string.Empty;
                        string newParams = string.Empty;
                        Params += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        newParams += item.ColumnName + ", ";
                        Params = (Params.Length > 0 ? Params = Params.Remove(Params.Length - 2, 2) : "");//Params.Remove(Params.Length - 1, 1);
                        newParams = newParams.Length > 1 ? newParams.Remove(newParams.Length - 2, 2) : "";
                        Code.AppendLine(string.Format(CsharpHelper.DalSelectRowFK, strStatic, spName + "ByForeignKey_" + item.ColumnName, Params, newParams));
                    }
                }
            }

            string newParamsTemp = string.Empty;
            string ParamsTemp = string.Empty;
            string ColumnName = string.Empty;
            foreach (var item in Columns)
            {

                if (item.TableName == TableName)
                {
                    if (item.IsForeignKey != null && int.Parse(item.IsForeignKey) != 0)
                    {


                        ParamsTemp += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        newParamsTemp += item.ColumnName + ", ";

                        ColumnName = ColumnName + "_" + item.ColumnName;
                        
                    }
                }
            }
            ParamsTemp = (ParamsTemp.Length > 0 ? ParamsTemp = ParamsTemp.Remove(ParamsTemp.Length - 2, 2) : "");//Params.Remove(Params.Length - 1, 1);
            newParamsTemp = newParamsTemp.Length > 1 ? newParamsTemp.Remove(newParamsTemp.Length - 2, 2) : "";
            if (ColumnName == "" || Code.ToString().IndexOf(spName + "ByForeignKey" + ColumnName) > -1)
                return Code.ToString();
            Code.AppendLine(string.Format(CsharpHelper.DalSelectRowFK, strStatic, spName + "ByForeignKey" + ColumnName, ParamsTemp, newParamsTemp));
            
            return Code.ToString();
        }
        #endregion

        #region InsertRow
        public static string InsertRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            StringBuilder Code = new StringBuilder();
            string strStatic = (Static ? "static " : "");
            string spName = TableName + "InsertRow";

            string Params = string.Empty;
            string newParams = string.Empty;

            bool firstParam = true;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (int.Parse(item.IsIdentity) == 0)
                    {
                        if (firstParam == true)
                        {
                            firstParam = false;
                        }
                        Params += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        newParams += item.ColumnName + ", ";
                    }
                }
            }
            Params = (Params.Length > 0 ? Params = Params.Remove(Params.Length - 2, 2) : "");
            newParams = (newParams.Length > 0 ? newParams = newParams.Remove(newParams.Length - 2, 2) : "");
            Code.AppendLine(string.Format(CsharpHelper.DalInsertRow, strStatic, TableName + MethodName, Params, spName, newParams));

            return Code.ToString();
        }
        #endregion

        #region UpdateRow
        public static string UpdateRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            StringBuilder Code = new StringBuilder();
            string strStatic = (Static ? "static " : "");
            string spName = TableName + "UpdateRow";

            string Params = string.Empty;
            string newParams = string.Empty;

            bool firstParam = true;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (firstParam == true)
                    {
                        firstParam = false;
                    }
                    Params += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                    newParams += item.ColumnName + ", ";
                }
            }
            Params = (Params.Length > 0 ? Params = Params.Remove(Params.Length - 2, 2) : "");
            newParams = (newParams.Length > 0 ? newParams = newParams.Remove(newParams.Length - 2, 2) : "");

            Code.AppendLine(string.Format(CsharpHelper.DalUpdateRow, strStatic, TableName + MethodName, Params, spName, newParams));

            return Code.ToString();
        }
        #endregion

        #region DeleteRow
        public static string DeleteRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            StringBuilder Code = new StringBuilder();
            string strStatic = (Static ? "static " : "");
            string spName = TableName + "DeleteRow";

            string Params = string.Empty;
            string newParams = string.Empty;

            bool firstParam = true;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (int.Parse(item.IsIdentity) != 0 || int.Parse(item.IsIndex) != 0)
                    {
                        if (firstParam == true)
                        {
                            firstParam = false;
                        }
                        Params += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        newParams += item.ColumnName + ", ";
                    }
                }
            }
            Params = (Params.Length > 0 ? Params = Params.Remove(Params.Length - 2, 2) : "");
            newParams = (newParams.Length > 0 ? newParams = newParams.Remove(newParams.Length - 2, 2) : "");

            Code.AppendLine(string.Format(CsharpHelper.DalDeleteRow, strStatic, TableName + MethodName, Params, spName, newParams));
            Code.AppendLine(Environment.NewLine + "\t}" + Environment.NewLine + "}");

            return Code.ToString();
        }
        #endregion
    }
}
